package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.New;
import com.entity.page.Page;
import com.entity.vo.NewVo;
import com.service.NewService;
import com.service.impl.NewServiceImpl;

/**
 * 控制层 MVC - C  为视图层提供数据 PC
 * 新闻分页控制器
 * 
 */
@WebServlet("/news_list")
public class NewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public NewsServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		String pageNo = request.getParameter("pageNo");
		//新闻标题
		String ntitle = request.getParameter("ntitle");
		String nauthor = request.getParameter("nauthor");//作者
		String ntid = request.getParameter("ntid");//新闻类型
		String ncreateDate = request.getParameter("ncreateDate");//日期
	
		if(ntitle==null){
			ntitle = "";
		}
		if(nauthor==null){
			nauthor = "";
		}
		if(ntid==null){
			ntid = "0";
		}
		if(ncreateDate==null){
			ncreateDate = "";
		}

		NewVo newVo = new NewVo(Integer.parseInt(ntid),ntitle,nauthor,ncreateDate);
		
		NewService newService = new NewServiceImpl();
		try {
			Page<New> pageInfo = newService.selectNewsByPage(pageNo == null ? 1 : Integer.parseInt(pageNo), 10,newVo);
			request.setAttribute("pageInfo", pageInfo);
			request.getRequestDispatcher("news_all.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
